﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DoAnB
{
    public partial class qlSanPham : Form
    {
        public qlSanPham()
        {
            InitializeComponent();
            string loaitp = "SELECT MaLoaiThucPham, LoaiThucPham FROM LoaiThucPham";
            ketnoi data = new ketnoi();
           
            comboBox1Loai.DataSource = data.getDataTable(loaitp);
            comboBox1Loai.ValueMember = "MaLoaiThucPham";
            comboBox1Loai.DisplayMember = "LoaiThucPham";

            //loadChiTietMonAn();
        }

        private void QuanLyThucAn_Load(object sender, EventArgs e)

        {
           
        }

        private void loadChiTietMonAn()
        {
            string ID = comboBox1Loai.SelectedValue.ToString();
            ketnoi data = new ketnoi();
            string sql = "Select * from SanPham where MaLoaiThucPham = "+ID;
            dgChiTiet.DataSource = data.getDataTable(sql);
            int i = dgChiTiet.RowCount;
            lblSoLuong.Text = i.ToString();
                
         
        }
      
        private void themClick(object sender, EventArgs e)
        {
            frmThemThucPham ttu = new frmThemThucPham();
            ttu.ShowDialog();
        }

        private void xoa_Click(object sender, EventArgs e)
        {
            if (dgChiTiet.DataSource == null)
                MessageBox.Show("Chưa chọn", "Thông báo", MessageBoxButtons.OK);
            else
            {
                DialogResult = MessageBox.Show("Bạn thật sự muốn xóa thức uống này chứ?", "Xác Nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
               
                if (DialogResult == DialogResult.Yes)
                {
                    string MaTU = dgChiTiet.SelectedRows[0].Cells[0].Value.ToString();
                    string sql = "delete from SanPham where ID = " + MaTU;
                    ketnoi data = new ketnoi();
                    data.ExeCuteNonQuery(sql);
                    loadChiTietMonAn();
                }
            }
           
        }

        private void dong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bttTimTU_Click(object sender, EventArgs e)
        {
            if (textBoxTimKiem.Text == null)
                MessageBox.Show("Chưa nhập", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else 
            {
                string sql = "SELECT * FROM SanPham WHERE Ten LIKE '%"+ textBoxTimKiem.Text +"%'";
                ketnoi data = new ketnoi();
                dgChiTiet.DataSource = data.getDataTable(sql);
                lblSoLuong.Text = dgChiTiet.RowCount.ToString();
            }
        }

        private void comboBox1Loai_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadChiTietMonAn();
        }

        
    }
}
